using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using EQEmuMysql;

namespace EQEmulatorGUI
{
    public partial class frmVariables : Form
    {
        private SortedList<string, Variables> lsVariables;
        private Variables objVariable;
        private bool flgVariablesLoaded = false;
        public frmVariables()
        {
            InitializeComponent();
        }
        public void VariablesList()
        {
            flgVariablesLoaded = false;
            ListViewItem lsvItem;
            lsvVariables.Items.Clear();

            if (flgVariablesLoaded == false)
            {
                lsVariables = Lists.VariableList();
                flgVariablesLoaded = false;
            }
            foreach (Variables oVariable in lsVariables.Values)
            {
                lsvItem = new ListViewItem(oVariable.VarName);
                lsvItem.Tag = oVariable.VarName;

                lsvVariables.Items.Add(lsvItem);
            }
        }
        private void frmVariables_Load(object sender, EventArgs e)
        {
            VariablesList();
        }

        private void btnVarSave_Click(object sender, EventArgs e)
        {

            objVariable.VarName = txtVarName.Text;
            objVariable.Value = txtValue.Text;
            objVariable.Information = txtInformation.Text;

            if (objVariable.Save() == true)
            {
                MessageBox.Show("Update Successfull.", "Update Variable");
                ClearVariable();
                VariablesList();
            }
            else
                MessageBox.Show("Update Failed.", "Update Variable");
        }
        private void ClearVariable() {
            txtVarName.Text = "";
            txtValue.Text = "";
            txtInformation.Text = "";
        }
        private void btnVarLoad_Click(object sender, EventArgs e)
        {
            VariablesList();
        }

        private void lsvVariables_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lsvVariables.SelectedItems.Count <= 0)
            {
                return;
            }

            objVariable = lsVariables[lsvVariables.SelectedItems[0].Tag.ToString()];

            txtVarName.Text = objVariable.VarName;
            txtValue.Text = objVariable.Value;
            txtInformation.Text = objVariable.Information;
            
        }
    }
}